﻿using System;
using System.Diagnostics;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace TeamDeploy.Tasks
{
    public class DeviceController : TaskBase
    {
        public DeviceController()
        {
            TurnOffOtherDevices = false;
        }

        public override bool Execute()
        {
            try
            {
                DateTime current = DateTime.Now;

                int starthours;
                int startminutes;
                int.TryParse(StartTime.Split(':')[0], out starthours);
                int.TryParse(StartTime.Split(':')[1], out startminutes);
                int stophours;
                int stopminutes;
                int.TryParse(EndTime.Split(':')[0], out stophours);
                int.TryParse(EndTime.Split(':')[1], out stopminutes);

                if(current.TimeOfDay < new TimeSpan(starthours, startminutes, 0) ||
                    current.TimeOfDay > new TimeSpan(stophours, stopminutes, 0))
                {
                    if (BuildEngine != null)
                    {
                        Log.LogMessage("Skipping DeviceController task. It is outside of start/stop times");
                        return true;
                    }
                }

                string turnoffArg = "";
                if (TurnOffOtherDevices)
                {
                    turnoffArg = " TurnOffOtherDevices";
                }


                string args = "\\\\" + TargetMachine + " -accepteula " + DeviceControllerAppPathFileName + " -hc:" + HouseCode + " -dc:" + DeviceCode + " -c:" + Command + turnoffArg;
                Process p = Process.Start(RemoteExecutePathFilename, args);
                p.WaitForExit();


                if (p.ExitCode != 0)
                {
                    string message = "DeviceController " + DeviceControllerAppPathFileName + " on " + TargetMachine + " returned code:" + p.ExitCode;

                    if (ignoreExitCode)
                    {
                        Log.LogMessage(message);
                    }
                    else
                    {
                        Log.LogError(message);
                        return false;
                    }
                }

                if (BuildEngine != null)
                {
                    Log.LogMessage("DeviceController " + DeviceControllerAppPathFileName + " on " + TargetMachine + " had been execute.");
                }
                return true;

            }
            catch (Exception ex)
            {
                if (BuildEngine != null)
                {
                    Log.LogError("Error executing DeviceController: DeviceController=" + DeviceControllerAppPathFileName + " TargetMachine=" +
                                 TargetMachine + " RemoteExecutePathFilename=" + RemoteExecutePathFilename + ":::" + ex);
                }
                return false;
            }
        }

        [Required]
        public string RemoteExecutePathFilename { get; set; }

        [Required]
        public string TargetMachine { get; set; }

        [Required]
        public string StartTime { get; set; }

        [Required]
        public string EndTime { get; set; }

        [Required]
        public string DeviceControllerAppPathFileName { get; set; }

        [Required]
        public string HouseCode { get; set; }

        [Required]
        public int DeviceCode { get; set; }

        [Required]
        public int Command { get; set; }

        public bool TurnOffOtherDevices { get; set; }

    }
}
